package org.dromara.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.domain.MallGoodsSpec;
import org.dromara.mall.domain.bo.MallGoodsSpecBo;
import org.dromara.mall.domain.vo.MallGoodsSpecVo;
import org.dromara.mall.service.IMallGoodsSpecService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格
 *
 * @author Lion Li
 * @date 2024-05-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/goodsSpec")
@Slf4j
public class MallGoodsSpecController extends BaseController {

    private final IMallGoodsSpecService mallGoodsSpecService;


    /*自定义根据商品分类查询列表*/
    @SaCheckPermission("mall:goodsSpec:list")
    @GetMapping("/pageList")
    public TableDataInfo<MallGoodsSpec> list(MallGoodsSpec bo, PageQuery pageQuery) {
        return mallGoodsSpecService.queryPageList(bo, pageQuery);
    }
    /*自定义根据商品id查询规格列表*/
    @GetMapping("/list")
    public R<List<MallGoodsSpec>> selectByGoodsIdList(MallGoodsSpec bo) {
        return R.ok(mallGoodsSpecService.selectByGoodsIdList(bo));
    }

    /**
     * 导出商品规格列表
     */
    @SaCheckPermission("mall:goodsSpec:export")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallGoodsSpec bo, HttpServletResponse response) {
        List<MallGoodsSpec> list = mallGoodsSpecService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品规格", MallGoodsSpec.class, response);
    }

    /**
     * 获取商品规格详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:goodsSpec:query")
    @GetMapping("/{id}")
    public R<MallGoodsSpecVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(mallGoodsSpecService.queryById(id));
    }


    /**
     * 新增商品规格
     */
    @SaCheckPermission("mall:goodsSpec:add")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallGoodsSpecBo bo) {
         return toAjax(mallGoodsSpecService.insertByBo(bo));
    }

    /**
     * 修改商品规格
     */
    //TODO 这个功能还没对，增加和修改前端要重新写传值方法
    @SaCheckPermission("mall:goodsSpec:edit")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallGoodsSpecBo bo) {
        return toAjax(mallGoodsSpecService.updateByBo(bo));
    }

    /*删除*/
    @DeleteMapping("/{ids}")
    @SaCheckPermission("mall:goodsSpec:remove")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                              @PathVariable Long[] ids) {
        return toAjax(mallGoodsSpecService.deleteWithValidByIds(List.of(ids), true));
    }
}
